let blogImg = null;
let blogImgSrc = null;
let blogTitle = null;
let postScript = null;
let blogAddBtn = null;
let blogAddForm = null;
let blogContent = null;


$(function () {
    blogImg = $('#blogImg');
    blogImgSrc = $('#blogImgSrc');
    blogTitle = $('#blogTitle');
    postScript = $('#postScript');
    blogAddBtn = $('#blogAdd-btn');
    blogAddForm = $('#blogAddForm');
    blogContent = $('#blogContent');

    blogTitle.on("blur", function () {
        if (blogTitle.val() == null || blogTitle.val().length < 1){
            validateTip(blogTitle.next(), {"color": "red"}, imageNo + "标题不可以为空", false);
        } else if (blogTitle.val().length > 0 && blogTitle.val().length < 20) {
            validateTip(blogTitle.next(), {"color": "green"}, null, true);
        } else {
            validateTip(blogTitle.next(), {"color": "red"}, imageNo + " 标题长度必须小于20", false);
        }
    }).focus();


    postScript.on("blur", function () {
        if (postScript.val() != null && postScript.val().length < 20) {
            validateTip(postScript.next(), {"color": "green"}, null, true);
        } else {
            validateTip(postScript.next(), {"color": "red"}, imageNo + " 备注长度必须小于20", false);
        }
    });

    blogImgSrc.on("change", function () {
        if (this.files.length > 0) {
            let file = this.files[0];
            let reader = new FileReader();

            reader.onload = function (e) {
                document.querySelector('#blogImg').src = this.result;
            }
            reader.readAsDataURL(file);
        }
    })

    blogAddBtn.on("click", function () {
        if (blogTitle.attr("validateStatus") != "true") {
            blogTitle.blur();
        } else if (postScript.attr("validateStatus") != "true") {
            postScript.blur();
        } else if (confirm("确认信息无误了吗？")) {
            blogAddForm.submit();
        }
    })

})